//
//  QYGMineViewController.m
//  云+物娱
//
//  Created by yiguo qi on 2017/11/29.
//  Copyright © 2017年 ICE. All rights reserved.
//

#import "QYGMineViewController.h"
#import "QYGPersonalView.h"
#import "QYGMineTableViewCell.h"
#import "QYGSetTableViewController.h"
#import "QYGAccountViewController.h"
#import "QYGFetchingRecordVC.h"
#import "QYGMineDollController.h"
#import "QYGWebViewController.h"
#import "AppDelegate.h"
#import "QYGConfigModel.h"
#import "QYGNetRequestModel.h"
#import "QYGLoginViewController.h"
#import "QYGCoinController.h"

static NSString *identifier = @"personalDataCell";
static NSString *optionCell = @"optionCell";


@interface QYGMineViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray  *dataSource;

@property (nonatomic, assign) NSInteger isShowPay;
@property (nonatomic, strong) QYGMineHeaderView *headerView;
@end

@implementation QYGMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cm_backgroudColor];
    self.isShowPay = [QYGKeychain isShowPay];
    [self setupView];
    self.dataSource = [self setPersonDateWithIsLogin:self.isShowPay];
    self.navigationController.delegate = self;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView  reloadData];
}
- (void)setUserInfo:(QYGUserInfoModel *)userInfo{
    _userInfo = userInfo;
    [self.tableView reloadData];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)setupView{
    [self.view addSubview:self.tableView];
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.x = 0;
    self.tableView.y = -STATUSBAR_HEIGHT;
    self.tableView.width = self.view.width;
    self.tableView.height = SCREEN_HEIGHT;
}
#pragma mark- UITableViewDelegate,DataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.dataSource[section];
    return array.count;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(!section){
        [self.headerView setNameStr:self.userInfo.nickname detailStr:[NSString stringWithFormat:@"ID: %ld",self.userInfo.userId] headerStr:self.userInfo.avatar];
        return self.headerView;
    }else{
        UIView *view = [[UIView  alloc] init];
        view.backgroundColor = section == 0 ? [UIColor whiteColor] : [UIColor clearColor];
        return view;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *sectionArr = self.dataSource[indexPath.section];
    NSDictionary *dic  = sectionArr[indexPath.row];
    QYGMineTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:optionCell forIndexPath:indexPath];
    cell.iconStr = dic[@"icon"];
    cell.nameStr = dic[@"title"];
    
    if(self.isShowPay && indexPath.row == 1){
        cell.detailStr = [NSString stringWithFormat:@"%ld",self.userInfo.coin];
    }else{
        cell.detailStr = nil;
    }
    cell.mineAccessoryType = QYGMineTableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == sectionArr.count - 1){
        cell.isHiddenLine = YES;
    }else{
        cell.isHiddenLine = NO;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScaleH(60);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(!section){
        return ScaleH(218);
    }else{
        return ScaleH(13);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 0.1f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic  = self.dataSource[indexPath.section][indexPath.row];
    NSInteger section = indexPath.section;
        switch (section) {
            case 0: {
                if (indexPath.row == 0) {
                    QYLog(@"我的娃娃");
                    QYGWebViewController *webViewVC = [[QYGWebViewController alloc] init];
                    webViewVC.urlStr = [NSString stringWithFormat:@"%@/?#/mywawa?api_token=%@",[QYGKeychain mall_url],[QYGKeychain token]];
                    webViewVC.titleStr = @"我的娃娃";
                    [self.navigationController pushViewController:webViewVC animated:YES];
                    
                } else if (indexPath.row == 1 ){
                    if(self.isShowPay){
                        QYLog(@"娃娃币");
                        QYGCoinController *coinVC = [[QYGCoinController alloc] init];
                        coinVC.userInfo = self.userInfo;
                        [self.navigationController pushViewController:coinVC animated:YES];
                    }else{
                        QYLog(@"抓取记录");
                        QYGFetchingRecordVC *recordVC = [[QYGFetchingRecordVC alloc] init];
                        [self.navigationController pushViewController:recordVC animated:YES];
                    }
                } else if (indexPath.row == 2) {
                    if(self.isShowPay){
                        QYLog(@"充值记录");
                        QYGAccountViewController *accountVC = [[QYGAccountViewController alloc] init];
                        accountVC.userInfo = self.userInfo;
                        [self.navigationController pushViewController:accountVC animated:YES];
                    }else{
                        
                        QYLog(@"我的订单");
                        QYGWebViewController *webViewVC = [[QYGWebViewController alloc] init];
                        webViewVC.urlStr = [NSString stringWithFormat:@"%@/?#/order-list?api_token=%@",[QYGKeychain mall_url],[QYGKeychain token]];
                        webViewVC.titleStr = @"我的订单";
                        [self.navigationController pushViewController:webViewVC animated:YES];
                    }
                } else if (indexPath.row == 3) {
                    if(self.isShowPay){
                        QYLog(@"抓取记录");
                        QYGFetchingRecordVC *recordVC = [[QYGFetchingRecordVC alloc] init];
                        [self.navigationController pushViewController:recordVC animated:YES];
                    }else{
                        QYLog(@"收获地址");
                        QYGWebViewController *webViewVC = [[QYGWebViewController alloc] init];
                        webViewVC.urlStr = [NSString stringWithFormat:@"%@/?#/address-list?api_token=%@",[QYGKeychain mall_url],[QYGKeychain token]];
                        webViewVC.titleStr = @"收货地址";
                        [self.navigationController pushViewController:webViewVC animated:YES];
                    }
                    
                }else if (indexPath.row == 4) {
                    
                    QYLog(@"我的订单");
                    QYGWebViewController *webViewVC = [[QYGWebViewController alloc] init];
                    webViewVC.urlStr = [NSString stringWithFormat:@"%@/?#/order-list?api_token=%@",[QYGKeychain mall_url],[QYGKeychain token]];
                    webViewVC.titleStr = @"我的订单";
                    [self.navigationController pushViewController:webViewVC animated:YES];
                }else if (indexPath.row == 5) {
                    
                    QYLog(@"收获地址");
                    QYGWebViewController *webViewVC = [[QYGWebViewController alloc] init];
                    webViewVC.urlStr = [NSString stringWithFormat:@"%@/?#/address-list?api_token=%@",[QYGKeychain mall_url],[QYGKeychain token]];
                    webViewVC.titleStr = @"收货地址";
                    [self.navigationController pushViewController:webViewVC animated:YES];
                }
                break;
            }
            case 1: {
                if (indexPath.row == 0) {
                    QYGSetTableViewController *setVC = [[QYGSetTableViewController alloc] init];
                    [self.navigationController pushViewController:setVC animated:YES];
                } else if (indexPath.row == 1) {
                } else if (indexPath.row == 2) {
                } else if (indexPath.row == 3) {
                }
                break;
            }
            case 3: {
                if (indexPath.row == 0) {
                } else if (indexPath.row == 1) {
                } else if (indexPath.row == 2) {
                }
                break;
            }
            default:
                break;
        }
}
#pragma mark - target

- (void)rightBarButtonItemClick{
    QYGSetTableViewController *setVC = [[QYGSetTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:setVC animated:YES];
}

#pragma mark - lazyLoad
- (QYGMineHeaderView *)headerView{
    if(!_headerView){
        _headerView = [[QYGMineHeaderView alloc] init];
    }
    return _headerView;
}
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor colorWithHex:@"f4f4f4"];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        CGFloat navH = self.navigationController.navigationBar.height;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, navH + 20, 0);
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.bounces = NO;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
        [_tableView registerClass:[QYGMineTableViewCell class] forCellReuseIdentifier:optionCell];
    }
    return _tableView;
}
- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSArray array];
    }
    return _dataSource;
}

#pragma mark-  列表数据 0未登录 1登录
- (NSArray *)setPersonDateWithIsLogin:(NSInteger)isShowPay {
    NSArray *personDate = [NSArray array];
    
    NSArray *exitDate = @[
                          @[@{@"title" : @"我的娃娃", @"icon"  : @"mine_wdww"},
                            @{@"title" : @"抓取记录", @"icon"  : @"mine_zqjl"},
                            @{@"title" : @"订单管理", @"icon"  : @"mine_wddd" },
                            @{@"title" : @"收货地址", @"icon"  : @"mine_shdz"}],
                           @[@{@"title" : @"设置"  ,  @"icon" : @"setting"}]
                          ];
    NSArray *loginDates = @[
                            @[ @{@"title" : @"我的娃娃",@"icon"  : @"mine_wdww"},
                               @{@"title" : @"娃娃币", @"icon"  : @"mine_wwb"},
                               @{@"title" : @"充值记录",@"icon"  : @"mine_czjl"},
                               @{@"title" : @"抓取记录", @"icon"  : @"mine_zqjl"},
                               @{@"title" : @"订单管理",@"icon"  : @"mine_wddd"},
                               @{@"title" : @"收货地址",@"icon"  : @"mine_shdz"}],
                            @[@{@"title" : @"设置",@"icon"   : @"setting"}]
                            ];
    
    if (!isShowPay){
        personDate = exitDate;
    } else {
        personDate = loginDates;
    }
    return personDate;
}
@end

